package top.yokey.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;

import com.google.gson.Gson;

import top.yokey.android.R;
import top.yokey.android.base.App;
import top.yokey.android.base.BaseActivity;
import top.yokey.android.http.BaseCallBack;
import top.yokey.android.http.HttpHelp;
import top.yokey.android.http.MemberBean;

public class LoginActivity extends BaseActivity {

    private AppCompatEditText mobileEditText;
    private AppCompatEditText passwordEditText;
    private AppCompatTextView loginTextView;
    private AppCompatTextView registerTextView;
    private View mobileView, passwordView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        super.onCreate(savedInstanceState);
        //从sharedP中读取 键名为 token 的值
        String token = App.get().getSharedPreferences().getString("token", "");
        System.out.println(token);
        //如果token不为空代表已经登录，不需要在展示登录界面，直接跳转到MainActivity
        if (!token.isEmpty()) getMemberData();

    }

    @Override
    public void initView() {

        mobileEditText = findViewById(R.id.mobileEditText);
        passwordEditText = findViewById(R.id.passwordEditText);
        loginTextView = findViewById(R.id.loginTextView);
        registerTextView = findViewById(R.id.registerTextView);
        mobileView = findViewById(R.id.mobileView);
        passwordView = findViewById(R.id.passwordView);

    }

    @Override
    public void initEvent() {

        //编辑框的焦点事件
        mobileEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.primary);
                passwordView.setBackgroundResource(R.color.textHint);
            }
        });
        passwordEditText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                mobileView.setBackgroundResource(R.color.textHint);
                passwordView.setBackgroundResource(R.color.primary);
            }
        });
        //单击事件
        loginTextView.setOnClickListener(view -> {
            //调用系统浏览器打开一个网址
            String mobile = mobileEditText.getText().toString();
            String password = passwordEditText.getText().toString();
            if (mobile.isEmpty() || password.isEmpty()) {
                Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
                return;
            }
            //禁用注册按钮，防止用户一直点点点
            loginTextView.setEnabled(false);
            loginTextView.setText("正在登录...");
            //使用HttpHelp发送请求
            HttpHelp.get().ready("base", "login")
                    .add("mobile", mobile)
                    .add("password", password)
                    .get(new BaseCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            //重新初始化Http框架，确保带上token去做后续的请求
                            HttpHelp.get().setToken(data);
                            //存储token
                            App.get().getEditor().putString("token", data);
                            App.get().getEditor().apply();
                            //获取用户数据
                            getMemberData();
                        }

                        @Override
                        public void onError(String reason) {
                            Toast.makeText(LoginActivity.this, reason, Toast.LENGTH_SHORT).show();
                            loginTextView.setEnabled(true);
                            loginTextView.setText("登录");
                        }
                    });
        });

        registerTextView.setOnClickListener(view -> {
            Intent intent = new Intent(this, RegisterActivity.class);
            intent.putExtra("mobile", "15207713074");//带上键名为mobile值为10086的键值对，
            startActivityForResult(intent, 1);
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1 && requestCode == 1) {
            String username = data.getStringExtra("username");
            mobileEditText.setText(username);
        }
    }

    //自定义函数

    private void getMemberData() {

        HttpHelp.get().ready("member", "getMemberData")
                .get(new BaseCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        App.get().setMemberBean(new Gson().fromJson(data, MemberBean.class));
                        //跳转到MainActivity
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        //结束当前
                        finish();
                    }

                    @Override
                    public void onError(String reason) {
                        Toast.makeText(LoginActivity.this, reason, Toast.LENGTH_SHORT).show();
                        if (reason.contains("登录")) {
                            App.get().getEditor().putString("token", "");
                            App.get().getEditor().apply();
                        }
                    }
                });

    }

}
